<?php

require ("utilities/utilsAppliTrombi.php") ;

connect() ;

// gestion des pages
$nombreRushParPage = 6 ;
if (!empty($_GET['p']))
   $page = $_GET['p'] ;
else
   $page = 1 ;
//echo time() ;

// Recherche de l'élève
$requete = "SELECT * FROM Eleve WHERE id='".$_GET['id']."';" ;
$result = executerRequete($requete, $GLOBALS['debug']) ;
if (mysql_num_rows($result) <= 0) return ;
$eleve = mysql_fetch_assoc($result);
$id = $eleve['id'] ;

// Supprimer
if (!empty($_GET["supprimer"]))
{
    if ($_GET["confirmer"] != "y")
    {
?>
<center>
Êtes-vous sur de vouloir supprimer ce rush ?
<a href="index.php?page=eleve&id=<?= $id ?>&supprimer=<?= $_GET["supprimer"] ?>&confirmer=y">Oui</a> -
<a href="index.php?page=eleve&id=<?= $id ?>">Non</a>
</center>
<br/>
<?php
    }
    else
    {
        $result = executerRequete("SELECT * FROM Rush WHERE id=\"".$_GET["supprimer"]."\";", $GLOBALS["debug"]) ;
        $row = mysql_fetch_assoc($result) ;
        // Suppression du fichier
        exec("mv -f eleve_trombi/".$row["lien"].".avi trash/") ;
        exec("mv -f eleve_trombi/".$row["lien"].".jpg trash/") ;
        // Suppression du rush
        executerRequete("DELETE FROM Rush WHERE id=\"".$_GET["supprimer"]."\";") ;
    }
}

// Déplacement de Rush
if (!empty($_GET["deplacer"]))
{
   if (!empty($_GET["vers"]))
   {
      $result = executerRequete ("SELECT id FROM Eleve WHERE login=\"".$_GET['vers']."\";", $GLOBALS["debug"]) ;
      if (mysql_num_rows($result) > 0)
      {
         $row = mysql_fetch_assoc($result) ;
         $idDest = $row['id'] ;
         executerRequete ("UPDATE Rush SET eleve=\"".$idDest."\" WHERE id=\"".$_GET["deplacer"]."\";", $GLOBALS['debug']) ;
      }
      else
         Alert ("Eleve introuvable !") ;
   }
   else
   {
      $result = executerRequete ("SELECT * FROM Rush WHERE id=\"".$_GET["deplacer"]."\" ;", $GLOBALS['debug']) ;
      $row = mysql_fetch_assoc($result) ;
?>
<center>
<?= $row['lien'] ?>.dv<br/>
<img src="eleve_trombi/<?= $row['lien'] ?>.jpg" width="284" height="232"/><br/>
<form name="idDest" action="index.php" method="get">
<input type="hidden" name="page" value="eleve"/>
<input type="hidden" name="id" value="<?= $_GET['id'] ?>"/>
<input type="hidden" name="deplacer" value="<?= $_GET['deplacer'] ?>"/>
<label for="vers">Déplacer vers : </label>
<input type="text" class="eleveDest" name="vers" value="" size="32"/>
<input type="submit" value="Déplacer"/>
<br/>
<a href="index.php?page=eleve&id=<?= $_GET['id'] ?>">Annuler</a>
<br/>
</center>
</form>
<script language="JavaScript">
// Autocompletion du champ de déplacement

function formatItemEleve(row) {
            return row[1] + " " + row[2] + " (" + row[3] + ")" ;
        }
function formatResultEleve(row) {
            return row[3] ; 
        }

function autocompletionChampEleve(champ)
{
   champ.autocomplete("ajax/autocompleteEleve.php", {width:260,selectFirst:false,formatItem:formatItemEleve,formatResult:formatResultEleve}) ;
}
autocompletionChampEleve($("form input.eleveDest")) ;

</script>
<br/>
<?php
   }
}

mysql_close() ;

// Transfert de fichier
if (is_uploaded_file($_FILES['fichier']['tmp_name']))
{
   ajouterRushs($_FILES['fichier'], array($eleve["login"]), array($_POST['debut']), array($_POST['fin']), array($_POST['qualite'])) ;
}

echo "<a href=\"index.php?page=listEleves\">Retour à la liste des élèves</a>" ;

connect() ;

// Eleve suivant et precedent pour le choix des rushs

if (!empty($_GET["choixrush"]))
{
?>
<table style="width:100%">
<tr>
<td style="text-align:left;">
<?php
// precedent
/*
$result_precedent = executerRequete("SELECT Eleve.id FROM Eleve INNER JOIN Rush ON Rush.eleve=Eleve.id WHERE Eleve.nom != \"Inconnu\" AND Eleve.id < \"".$_GET['id']."\" AND Eleve.choisi=0 GROUP BY Eleve.id ORDER BY Eleve.id DESC;", $GLOBALS['debug']) ;
$row_precedent = mysql_fetch_assoc($result_precedent) ;
$id_precedent = $row_precedent['id'] ;*/
if (!empty($_GET['precedent']))
   echo "<a href=\"index.php?page=eleve&id=".$_GET['precedent']."&choixrush=1\"><h3>Pr&eacute;c&eacute;dent</h3></a>".PHP_EOL ;
?>
</td>
</tr>
<tr>
<td style="text-align:right;">
<?php
// suivant
$result_suivant = executerRequete("SELECT Eleve.id FROM Eleve INNER JOIN Rush ON Rush.eleve=Eleve.id WHERE Eleve.nom!=\"Inconnu\" AND Eleve.id > \"".$_GET['id']."\" AND Eleve.rush<0 GROUP BY Eleve.id;", $GLOBALS['debug']) ;
$row_suivant = mysql_fetch_assoc($result_suivant) ;
$id_suivant = $row_suivant['id'] ;
if (!empty($id_suivant))
   echo "<a href=\"index.php?page=eleve&id={$id_suivant}&choixrush=1&precedent=".$_GET['id']."\"><h3>Suivant</h3></a>".PHP_EOL ;
?>
</td></tr></table>
<?php
} // if choixrush=1

echo "<center><h1>".$eleve['prenom']." ".$eleve['nom']."</h1><br/></center>" ;

$f =  file("https://www.frankiz.net/tol/apv/".$eleve["login"]) ;
$adresse_image = "https://www.frankiz.net/".$f['0'] ;

echo "<div class=\"photo_trombi\" style=\"width:100%;text-align:center;\"><img class=\"phototrombi\" src=\"".$adresse_image."\" style=\"height:200px;\"/><br/></div>" ;

$tchecked = "" ;
if ($eleve['timeline']>0) $tchecked = " checked" ;
?>
<center>
<a href="index.php?page=randomRush&id=<?= $eleve['id'] ?>">Télecharger le rush</a><br/> 
<input type="checkbox" class="timeline" value="1" name="timeline"<?= $tchecked ?>/> Sur la timeline<br/>
</center>

<script language="JavaScript">
// Photo trombi originale quand on clique
var original = false ;
$("img.phototrombi").click(function(){
   return ;
   if (original || true)
   {
      $("img.phototrombi").attr("src", "https://www.frankiz.net/<?= $adresse_image ?>") ;
   }
   else
   {
      $("img.phototrombi").attr("src", "http://frankiz/trombino.php?original&image=true&login=<?= $eleve['login'] ?>&promo=2008") ;
   }
   original = !original ;
}) ;

// Sur la timeline
$("input.timeline").click(function(){
   var checked = 0 ;
   if ($(this).attr("checked"))
   { checked = 1 ; }
   $.post("ajax/changeTimeline.php", {id:<?= $eleve['id'] ?>, t:checked}) ;
}) ;

</script>
<?php

// Liste des rush
$result = executerRequete("SELECT COUNT(id) AS n FROM Rush WHERE eleve='$id' ;", $GLOBALS['debug']) ;
$nb = mysql_fetch_assoc($result) ;
$numRushs = $nb['n'] ;
$numPages = ceil(floatval($numRushs)/$nombreRushParPage) ;
$offset = ($page-1)*$nombreRushParPage ;
$result = executerRequete("SELECT * FROM Rush WHERE eleve='$id' LIMIT $offset, $nombreRushParPage ;", $GLOBALS['debug']) ;
?>
<br/>
<div class="listRushs" style="width:100%;text-align:center;">
<table class="liste listRushs" style="margin-left:auto;margin-right:auto;">
<tr>
   <th colspan="2">Liste des rushs</th>
</tr>
<?php if ($numPages > 1) { ?>
<tr>
   <td colspan="2">
<center>
<b>page : </b>
<?php
for ($i = 1 ; $i<=$numPages ; $i++)
{
   if ($i>1) echo " - " ;
   if ($i == $page)
      echo "<b>{$i}</b>".PHP_EOL ;
   else
      echo "<a href=\"index.php?page=eleve&id=".$_GET['id']."&p={$i}\">{$i}</a>".PHP_EOL ;
   
}
?>
</center>
   </td>
</tr>
<?php } ?>
<tr><td><br/></td></tr>
<tr>
<?php
$compte = 1 ;
while ($row = mysql_fetch_assoc($result))
{

?>
   <td style="width:400px;margin:10px;">
<center>
<div id="player<?= $row['id'] ?>">
<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="284" height="232" class="apercu"> 
		<param name="movie" value="player.swf" /> 
		<param name="allowfullscreen" value="true" /> 
		<param name="allowscriptaccess" value="always" /> 
		<param name="flashvars" value="file=eleve_trombi/<?= urlencode($row['lien']) ?>.flv&image=eleve_trombi/<?= urlencode($row['lien']) ?>.jpg&volume=0&controlbar=none" /> 
		<embed 
			type="application/x-shockwave-flash"
			id="player2"
			name="player2"
			src="player.swf" 
			width="284" 
			height="232"
			allowscriptaccess="always" 
			allowfullscreen="true"
			flashvars="file=eleve_trombi/<?= urlencode($row['lien']) ?>.flv&image=eleve_trombi/<?= urlencode($row['lien']) ?>.jpg&volume=0&controlbar=none" 
		/> 
	</object> 
</div>
<br/>
      Qualité :
<select class="changeQualite" name="<?= $row['id'] ?>">
<?php
for ($i=2 ; $i>=0 ; $i--)
{
   $select = "" ;
   if ($i == $row['qualite']) $select = " selected" ;
   echo "<option value=\"$i\"".$select.">".qualiteRush($i)."</option>".PHP_EOL ;
}
?>
</select>
<!-- <b><?= qualiteRush($row['qualite']) ?></b> -->
<br/>
<!-- <a href="eleve_trombi/<?= $row['lien'] ?>.dv"><img src="images/icones/telecharger.png"/></a> -->
<a href="index.php?page=eleve&id=<?= $id ?>&supprimer=<?= $row['id'] ?>"><img src="images/icones/supprimer.png"/></a>
<a href="index.php?page=eleve&id=<?= $id ?>&deplacer=<?= $row['id'] ?>"><img src="images/icones/deplacer.png"/></a><br/>
<?php
$checked = "" ;
if ($eleve['rush'] == $row['id']) $checked = " checked" ;
?>
<b><input type="radio" class="choixRush" name="choixRush" id="choixRush_<?= $row['id'] ?>" value="<?= $row['id'] ?>"<?= $checked ?>/><label for="choixRush_<?= $row['id'] ?>"> Choix</label></b>
</center>
<br/>
   </td>
<?php
if ($compte % 2 == 0) echo "</tr><tr>" ;
$compte = $compte + 1 ;
}
?>
</tr>
<?php if ($numPages > 1) { ?>
<tr>
   <td colspan="2">
<center>
<b>page : </b>
<?php
for ($i = 1 ; $i<=$numPages ; $i++)
{
   if ($i>1) echo " - " ;
   if ($i == $page)
      echo "<b>{$i}</b>".PHP_EOL ;
   else
      echo "<a href=\"index.php?page=eleve&id=".$_GET['id']."&p={$i}\">{$i}</a>".PHP_EOL ;
   
}
?>
</center>
   </td>
</tr>
<?php } ?>
</table>
</div>
<script language="JavaScript">
// Gestion de la mise à jour de la qualite d'un rush
$("select.changeQualite").change(function(){
   $.post("ajax/changeQualiteRush.php", {id: $(this).attr("name"),qualite: $(this).val()} ) ;
}) ;

// Gestion du choix du rush
$("input.choixRush").click(function(){
   $.post("ajax/choixRush.php", {id: $(this).val()}) ;
}) ;

</script>
<br/>
<?php

mysql_close() ;
?>
<form name="ajouterRush" method="post" action="index.php?page=eleve&id=<?= $id ?>" enctype="multipart/form-data">
<table class="form">
   <tr>
      <th colspan="2">Ajouter un nouveau rush</th>
   </tr>
   <tr>
      <td><label for="fichier">Transférer un fichier</label></td>
      <td><input type="file" name="fichier" value=""/></td>
   </tr>
   <tr>
      <td>Début et fin (format mm:ss)</td>
      <td><input type="text" name="debut" size="5" maxlength="5"> - 
<input type="text" name="fin" size="5" maxlength="5"></td>
   </tr>
   <tr>
      <td>Qualité</td>
      <td>
<select name="qualite">
<?php
for ($i=2 ; $i>=0 ; $i--)
{
   echo "<option value=\"$i\">".qualiteRush($i)."</option>".PHP_EOL ;
}
?>
</select>
      </td>
   </tr>
   <tr>
      <td colspan="2" style="center"><input type="submit" value="Envoyer"/></td>
   </tr>
</table>
</form>
